const { sequelize } = require("../db/index")
const { DataTypes, Model } = require("sequelize")

class UserRoomHistory extends Model {
  static async createHistory({
    roomId,
    userId,
    checkTime
  }) {
    try {
      return await UserRoomHistory.create({
        roomId,
        userId,
        checkTime
      })
    } catch (error) {
      console.log(error)
      throw new Error("创建入住记录失败")
    }
  }
}

UserRoomHistory.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    roomId: {
      comment: "房间id",
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    userId: {
      comment: "用户id",
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    checkTime: {
      comment: "入住宿舍时间",
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: new Date()
    }
  },
  {
    sequelize,
    modelName: "userRoomHistory",
    paranoid: true
  }
)

module.exports = UserRoomHistory
